package org.rcgwt.client.ui.grid.provider;

import java.util.List;

import org.rcgwt.client.model.IsReflected;
import org.rcgwt.client.ui.grid.model.IDataModel;

import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * 
 * A RPC data provider {@link IRowsProvider} for asynchronous call with
 * {@link IsReflected} object. This provider insert new object
 * {@link IsReflected} into {@link IDataModel}
 * 
 * @param <R>
 *            the result type of the service call (e.g. : List<IsReflected>).
 */
public abstract class AbstractRpcRowsProvider<T> implements IRowsProvider<T> {

    /**
     * Subclass this method to call the actual RPC service.
     * 
     * @param callback
     *            DataProvider callback.
     */
    protected abstract void load(AsyncCallback<List<T>> callback);

    @Override
    public void load(IRowLoadedCallback<T> callback) {
        load(callback, 0, -1);
    }

    @Override
    public void load(final IRowLoadedCallback<T> callback, final int start,
            final int count) {

        load(new AsyncCallback<List<T>>() {

            @Override
            public void onFailure(Throwable caught) {
                reportFailure(callback, start, count);
            }

            @Override
            public void onSuccess(List<T> result) {
                callback.processRowInserted(start, result);
            }

        });
    }

    protected void reportFailure(IRowLoadedCallback<T> callback, int start,
            int count) {
        // TODO
    }
}
